# -*- coding:utf-8 -*-
import requests

from utils.config.readConfig import ReadConfig

localReadConfig = ReadConfig()

class ConfigHttp:

    def __init__(self,httpname):
        global scheme, host, port, timeout
        self.httpname=httpname
        scheme = localReadConfig.get_http(self.httpname,"scheme")
        host = localReadConfig.get_http(self.httpname,"baseurl")
        port = localReadConfig.get_http(self.httpname,"port")
        timeout = localReadConfig.get_http(self.httpname,"timeout")
        self.headers = {}
        self.params = {}
        self.data = {}
        self.url = None
        self.files = {}
        self.state = 0


    def set_url(self, url):
        """
        set url
        :param: interface url
        :return:
        """
        self.url = scheme+'://'+host+":"+port+url

    def set_headers(self, header):
        """
        set headers
        :param header:
        :return:
        """
        self.headers = header

    def set_params(self, param):
        """
        set params
        :param param:
        :return:
        """
        self.params = param

    def set_data(self, data):
        """
        set data
        :param data:
        :return:
        """
        self.data = data

    # defined http get method
    def get(self):
        """
        defined get method
        :return:
        """
        try:
            response = requests.get(self.url, headers=self.headers, params=self.params, timeout=float(timeout))
            return response
        except requests.ConnectTimeout:
            print("get Time out!")
            return None

    # defined http post method
    # include get params and post data
    # uninclude upload file
    def post(self):
        """
        defined post method
        :return:
        """
        try:
            response = requests.post(self.url, headers=self.headers, params=self.params, data=self.data, timeout=float(timeout),verify=False)
            return response

        except requests.ConnectTimeout:
            print("post Time out!")
            return None

    # defined http post method
    # include upload file
    def postWithFile(self):
        """
        defined post method
        :return:
        """
        try:
            response = requests.post(self.url, headers=self.headers, data=self.data, files=self.files, timeout=float(timeout))
            return response
        except requests.ConnectTimeout:
            print("postWithFile Time out!")
            return None

    # defined http post method
    # for json
    def postWithJson(self):
        """
        defined post method
        :return:
        """
        try:
            response = requests.post(self.url, headers=self.headers, json=self.data, timeout=float(timeout))
            return response
        except requests.ConnectTimeout:
            print("postWithJson Time out!")
            return None

    def commonget(self, url, header={},params={}):
        """
        :param url:
        :param header:
        :param data:
        :return: status, message, result
        """
        self.set_url(url)
        self.set_headers(header)
        self.set_params(params)
        re=self.get()
        return re

    def commonpost(self, url, header={}, data={}):
        """
        :param url:
        :param header:
        :param data:
        :return: status, message, result
        """
        self.set_url(url)
        self.set_headers(header)
        self.set_data(data)
        re=self.post()
        return re

if __name__ == "__main__":
    configHttpzzbiao = ConfigHttp(httpname="HTTPfljrzzbiao")
    url = "/api/a72b00a7fd5d"
    data = {
        "Amount": 2350.67,
        "UserId": 42949882542,
        #"UserId": 42949877869,
        "CouponId": "00000000-0000-0000-0000-000000000000",
        "UseAwardRate": 1,
        "ClaimConveyId": "467a3c12-a629-4da4-ab4e-a9aa00000caf",
        "ApplyTime": "2018-12-20 17:00:00",
        "BookingCode":"",
        #债转标0，轻松投1,5轻松投复投，10竞价
        "InvestConsumeType":0,
        #手动买标1，自动投标2
        "InvestSource":1,
        "ClientPlatform": 1
    }
    tt=configHttpzzbiao.commonpost(url,data=data)
    print (tt.text)
